Skip to contentMethod: dumpAndAssertResults(String, Collection)
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.commons.test;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.List;
33: import java.util.Optional;
34: import java.util.stream.Stream;
35: import java.io.IOException;
36: import java.nio.charset.StandardCharsets;
37: import java.nio.file.Files;
38: import java.nio.file.Path;
39: import java.nio.file.Paths;
40: import org.eclipse.rdf4j.repository.Repository;
41: import org.eclipse.rdf4j.repository.RepositoryConnection;
42: import org.eclipse.rdf4j.rio.RDFFormat;
43: import it.tidalwave.util.As;
44: import it.tidalwave.role.SimpleComposite;
45: import it.tidalwave.bluemarine2.util.Dumpable;
46: import lombok.NoArgsConstructor;
47: import lombok.extern.slf4j.Slf4j;
48: import static it.tidalwave.util.test.FileComparisonUtilsWithPathNormalizer.*;
49: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
50: import static lombok.AccessLevel.PRIVATE;
51:
52: /***********************************************************************************************************************
53: *
54: * @author Fabrizio Giudici
55: *
56: **********************************************************************************************************************/
57: @NoArgsConstructor(access = PRIVATE) @Slf4j
58: public class TestUtilities
59: {
60: /*******************************************************************************************************************
61: *
62: ******************************************************************************************************************/
63: public static void dumpAndAssertResults (@Nonnull final String fileName, @Nonnull final Collection<?> data)
64: throws IOException
65: {
66: final Path actualResult = Paths.get("target", "test-results", fileName);
67: final Path expectedResult = Paths.get("target", "test-classes", "expected-results", fileName);
68: Files.createDirectories(actualResult.getParent());
69: final Stream<String> stream = data.stream().map(Object::toString);
70: Files.write(actualResult, (Iterable<String>)stream::iterator, StandardCharsets.UTF_8);
71: assertSameContents(expectedResult, actualResult);
72: }
73:
74: /*******************************************************************************************************************
75: *
76: ******************************************************************************************************************/
77: @Nonnull
78: public static List<String> dump (@Nonnull final As entity)
79: {
80: final List<String> result = new ArrayList<>();
81: result.add((entity instanceof Dumpable) ? ((Dumpable)entity).toDumpString() : entity.toString());
82: final Optional<SimpleComposite> composite = entity.maybeAs(_SimpleComposite_);
83: composite.ifPresent(c -> c.findChildren().results().forEach(child -> result.addAll(dump((As)child))));
84:
85: return result;
86: }
87:
88: /*******************************************************************************************************************
89: *
90: ******************************************************************************************************************/
91: public static void loadRepository (@Nonnull final Repository repository, @Nonnull final Path path)
92: throws Exception
93: {
94: log.info("loadRepository(..., {})", path);
95:
96: try (final RepositoryConnection connection = repository.getConnection())
97: {
98: connection.add(path.toFile(), null, RDFFormat.N3);
99: connection.commit();
100: }
101: }
102: }